package com.zzcm.channelinfo.control;

import java.util.ArrayList;
import com.zzcm.channelinfo.appmanager.AppManager;
import com.zzcm.channelinfo.runcommand.RunCommand;
import com.zzcm.channelinfo.tools.Tools;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Environment;
import android.util.Log;

/**
 * 主框架接口模块
 * @author 张少志
 * 本功能已经基本稳定
 */
public class SilentInstallationControl {
    
    private static SilentInstallationControl mSAControl = null;
    private Context mContext = null;
    private AppManager mAppManager;//应用管理
    private RunCommand mRunCommand;//执行命令行管理类
    public static final String SHOW_LOG = "showLog";//action转接参数
    
    //应用管理主要参数
    public static final class AppManagerParameter {
        public static final String SDCARD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();//SD卡路径
        public static final String DATA_PATH = "/data/data";//本应用授权路径
        //public static final String PACKAGE_NAME = "com.zzcm.yyassistant.client.home";//应用包名
        //public static final String APPLICATION_PATH = DATA_PATH+"/"+PACKAGE_NAME;//本应用授权路径
        public static final String INSTALL_APP = "com.zzcm.channelinfo.ZZCMIAPP";//安装应用
        public static final String CANCEL_INSTALL_APP = "com.zzcm.channelinfo.ZZCMCIAPP";//取消安装应用
        public static final String UNINSTALL_APP = "com.zzcm.channelinfo.ZZCMUAPP";//卸载应用
        public static final String CANCEL_UNINSTALL_APP = "com.zzcm.channelinfo.ZZCMCUAPP";//取消卸载应用
        public static final String CANCEL_ALL="cancelAll";//取消所有应用的安装或卸载
        public static final String CANCEL_LIST="cancelList";//取消一个链表中的所有应用的安装或卸载
        public static final String CANCEL_UNIQUEKEY="cancelUniqueKey";//取消一个应用的安装或卸载
        public static final String INSTALL_APP_SUCCESS = "com.zzcm.channelinfo.ZZCMIAPP_SUCCESS";//应用安装成功时，发送广播
        public static final String INSTALL_APP_FAIL = "com.zzcm.channelinfo.ZZCMIAPP_FAIL";//应用安装失败时，发送广播
        public static final String UNINSTALL_APP_SUCCESS = "com.zzcm.channelinfo.ZZCMUAPP_SUCCESS";//应用卸载成功时，发送广播
        public static final String UNINSTALL_APP_FAIL = "com.zzcm.channelinfo.ZZCMUAPP_FAIL";//应用卸载失败时，发送广播
        public static final String INSTALL_APP_COMMAND = "pm install -r ";//应用强制安装命令
        public static final String UNINSTALL_APP_COMMAND = "pm uninstall -r ";//应用强制卸载命令
        public static final String TYPE = "type";//执行命令的方式
        public static final String INSTALL_LOCATION = "installLocation";//默认安装位置 0 系统自动，1安装在手机内部存储，2安装到SD卡
        public static final String ROOT = "root";//root标志，用root权限安装或卸载
        public static final String DELETE = "delete";//安装后是否删除原文件
        public static final String APP_PATH = "appPath";//应用安装路径
        public static final String APP_PACKAGE_NAME = "appPackageName";//应用卸载包名
        public static final String INSTALL_TIMEOUT_RESULT = "INSTALL_TIMEOUT_RESULT";//安装超时
        public static final String INSTALL_APPPATH_NOTEXSISTS_RESULT = "INSTALL_APPPATH_NOTEXSISTS_RESULT";//应用路径不存在
        public static final String UNINSTALL_TIMEOUT_RESULT = "UNINSTALL_TIMEOUT_RESULT";//卸载超时
        public static final String UNINSTALL_APPPACKAGENAME_NOTEXSISTS_RESULT = "UNINSTALL_APPPACKAGENAME_NOTEXSISTS_RESULT";//应用路径不存在
    }
    
    //执行命令管理主要参数
    public static final class RunCommandParameter {
        public static final String RESULT = "result";//执行命令结果
        public static final String COMMON_USER_COMMAND = "sh ";//执行普通命令列表
        public static final String SUPER_USER_COMMAND = "su ";//申请ROOT权限
        public static final String RUN_COMMAND = "com.zzcm.channelinfo.ZZCMRCMD";//执行命令行
        public static final String RUN_COMMAND_RESULT = "com.zzcm.channelinfo.ZZCMRCMD_RESULT";//命令行的执行结果
        public static final String COMMAND_LIST = "commandList";//命令行列表
        public static final String ID = "id";//单次执行命令行的编号
        public static final String RUNCOMMAND_TIMEOUT_RESULT = "ZZCMRCMD_TIMEOUT_RESULT";//执行命令行超时
    }
    
    public SilentInstallationControl() {
        
    }
    
    /**
     * 获取单例模式
     * @return
     */
    public static SilentInstallationControl getInstence() {
        if(mSAControl==null)
        {
            mSAControl = new SilentInstallationControl();
        }
        return mSAControl;
    }
    
    /**
     * 绑定Context
     * @param context
     */
    public void setContext(Context context) {
        mContext = context;
        if(mSAControl!=null)
        {
            if(mAppManager!=null)
            {
                mAppManager.setContext(context);
            }
            if(mRunCommand!=null)
            {
            	mRunCommand.setContext(context);
            }
        }
    }
    
    private boolean init = false;
    /**
     * 系统初始化
     * @return
     */
    public boolean systemInit() {
    	if(!init)
    	{
    		Tools.showLog("systemInit", "begin");
    		init = true;
	        if(mContext==null)
	        {
	            return false;
	        }
	        actionManagerInit();
	        Tools.showLog("systemInit", "end");
    	}
        return true;
    }
    
    /**
     * 退出系统
     */
    public void systemExit() {
    	if(init)
    	{
    		Tools.showLog("systemExit", "begin");
    		actionManagerUnInit();
    		init = false;
    		Tools.showLog("systemExit", "end");
    	}
        mContext=null;
        mSAControl = null;
    }
    
    /**
     * 初始化所有子模块
     */
    private void actionManagerInit() {
        mAppManager = new AppManager(mContext);
        mRunCommand = new RunCommand(mContext);
    }
    
    /**
     * 卸载所有子模块
     */
    private void actionManagerUnInit() {
    	if(mAppManager!=null)
    	{
    		mAppManager.appManagerUnInit();
    		mAppManager = null;
    	}
    	if(mRunCommand!=null)
    	{
    		mRunCommand.runCommandUnInit();
    		mRunCommand = null;
    	}
    }
    
    private int startThreadCount = 0;//当前已经启动的线程数
    private synchronized int addNewStartThreadCount(int add) {
    	if(add!=0)
    	{
    		startThreadCount += add;
    	}
    	return startThreadCount;
    }
    /**
     * 开启新的线程
     */
    public void startNewThread() {
    	addNewStartThreadCount(1);
    	Tools.showLog("startNewThread", "startThreadCount="+startThreadCount);
    }
    
    /**
     * 销毁已有线程
     */
    public void destroyOldThread() {
    	addNewStartThreadCount(-1);
    	Tools.showLog("destroyOldThread", "startThreadCount="+startThreadCount);
    	if(addNewStartThreadCount(0)==0)//当所有启动的线程被销毁后，退出系统。
    	{
    		Tools.showLog("destroyOldThread", "systemExit and startThreadCount="+startThreadCount);
    		systemExit();
    	}
    }
    
    /**
     * 对外安装应用的公有接口
     * @param appPath
     */
    public void installApp(String appPath,boolean root,boolean delete,int location) {
        mAppManager.installApp(appPath,root,delete,location);
    }
    
    /**
     * 中止所有应用安装
     * @return
     */
    public boolean removeAllInstallApp() {
    	return mAppManager.removeAllInstallApp();
    }
    
    /**
     * 移除指定路径的安装软件
     * @param appPath
     * @return
     */
    public boolean removeInstallApp(String appPath) {
    	return mAppManager.removeInstallApp(appPath);
    }
    
    /**
     * 对外卸载应用的公有接口
     * @param appPackageName
     */
    public void unInstallApp(String appPackageName,boolean root) {
        mAppManager.unInstallApp(appPackageName,root);
    }
    
    /**
     * 中止所有软件被卸载
     * @return
     */
    public boolean removeAllUninstallApp() {
    	return mAppManager.removeAllUninstallApp();
    }
    
    /**
     * 移除指定包名的软件被卸载
     * @param appPath
     * @return
     */
    public boolean removeUninstallApp(String appPackageName) {
    	return mAppManager.removeUninstallApp(appPackageName);
    }
    
    /**
     * 执行一句或多句命令行
     * @param commandList
     */
    public void runCommand(String id,ArrayList<String> commandList,boolean root) {
    	mRunCommand.runCommand(id,commandList,root);
    }
}
